#include "functions.h"

SDL_Surface *screen;
TTF_Font *font;
TTF_Font *menu_font;
SDL_Surface *text_surface;
SDL_Color fcolor = {255,255,255};
const SDL_VideoInfo *dispinfo;

void InitSDL() {
	SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);
	TTF_Init();
	dispinfo = SDL_GetVideoInfo();
	screen = SDL_SetVideoMode(640, 480,32,SDL_DOUBLEBUF);
	atexit(SDL_Quit);
	SDL_EnableKeyRepeat(300, 50);
	SDL_EnableUNICODE(1);
	font=TTF_OpenFont("rna_font.ttf", 480/20);
	menu_font=TTF_OpenFont("rna_font.ttf", 1080/25);
	if (!font) {
		printf("Error al cargar la fuente\n");
	}
	if (!menu_font) {
		printf("Error al cargar la fuente\n");
	}
}
void free_surface(SDL_Surface* surf){
	if(surf) SDL_FreeSurface(surf);
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip) {
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface( source, clip, destination, &offset );
}
void flip_surface(SDL_Surface* surf) {
    SDL_Surface *aux;
    aux=rotozoomSurface(surf, 0, 0.3333333333, 1);
    SDL_Rect pos;
    pos.x=0;
    pos.y=(480-aux->h)/2;
    SDL_BlitSurface(aux ,NULL,screen,&pos);
    SDL_FreeSurface(aux);
    aux=NULL;
	SDL_Flip(screen);
}
void fontWrite(int x, int y, const char* text, SDL_Surface *surf, TTF_Font *font) {
	if (text != NULL) {
		text_surface=TTF_RenderText_Solid(font, text, fcolor);
		apply_surface(x, y, text_surface, surf, NULL);
		free_surface(text_surface);
	} else {
		exit(0);
	}
}
void fontPrintf(int x, int y, const char *format, ...) {
    va_list opt;
    char buff[2048];
    va_start(opt, format);
    vsnprintf(buff, sizeof(buff), format, opt);
    va_end(opt);
	fontWrite(x, y, buff, screen, font);
}

void fontPrintf_s(SDL_Surface *surf, int x, int y, const char *format, ...) {
    va_list opt;
    char buff[2048];
    va_start(opt, format);
    vsnprintf(buff, sizeof(buff), format, opt);
    va_end(opt);
	fontWrite(x, y, buff, surf, menu_font);
}

